package com.itheima.homework.controller;

import com.itheima.homework.pojo.Student;
import com.itheima.homework.service.StudentService;
import com.itheima.homework.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 注册用户
     *
     * @param student
     * @return
     */
    @RequestMapping("/users/register")
    public Result register(@RequestBody Student student) {
        String password = student.getPassword();
        if (Objects.isNull(password)) {
            student.setPassword("123456");
        }
        Integer gender = student.getGender();
        if (gender != null && Arrays.stream(Student.StudentGender.values()).noneMatch(studentGender -> studentGender.gender == gender)) {
            return Result.error("性别不符合要求");
        }
        if (password.length() < 6 || password.length() > 16) {
            return Result.error("密码不符合要求");
        }
        boolean usernameExist = studentService.existUserByUsername(student.getUsername());
        if (usernameExist) {
            return Result.error("用户已存在");
        }
        Student registerStudent = studentService.register(student);
        return Result.success(registerStudent);
    }

    /**
     * 登录
     *
     * @param student
     * @return
     */
    @RequestMapping("/users/login")
    public Result login(@RequestBody Student student) {
        boolean usernameExist = studentService.existUserByUsername(student.getUsername());
        if (usernameExist) {
            return Result.error("用户不存在");
        }
        Student existOne = studentService.getOneByUsername(student.getUsername());
        if (!Objects.equals(existOne.getPassword(), student.getPassword())) {
            return Result.error("密码不正确");
        }
        return Result.success(existOne.desensitization());
    }

    /**
     * 根据用户名查询学生信息
     *
     * @param username
     * @return
     */
    @RequestMapping("/users/{username}")
    public Result queryByUsername(@PathVariable String username) {
        Student existOne = studentService.getOneByUsername(username);
        if (Objects.isNull(existOne)) {
            return Result.error("用户不存在");
        }
        return Result.success(existOne.desensitization());
    }

    /**
     * 根据用户名进行用户删除
     *
     * @param username
     * @return
     */
    @RequestMapping("/users/del")
    public Result delByUsername(@RequestParam String username) {
        studentService.removeByUsername(username);
        return Result.success();
    }

    /**
     * 根据用户名修改学生密码
     *
     * @param student
     * @return
     */
    @RequestMapping("/users/updatePassword")
    public Result updatePassword(@RequestBody Student student) {
        Student existUserByUsername = studentService.getOneByUsername(student.getUsername());
        if (Objects.isNull(existUserByUsername)) {
            return Result.error("用户不存在");
        }
        if (!Objects.equals(existUserByUsername.getPassword(), student.getPassword())) {
            return Result.error("密码不正确");
        }
        String password = student.getPassword();
        if (Objects.isNull(password) || password.length() < 6 || password.length() > 16) {
            return Result.error("密码不符合要求");
        }
        studentService.updatePassword(student);
        return Result.success();
    }

    /**
     * 查询所有注册学生的列
     *
     * @return
     */
    @RequestMapping("/users/list")
    public Result listUser() {
        List<Student> list = studentService.list();
        return Result.success(list);
    }

    /**
     * 根据用户名修改学生信息
     *
     * @param student
     * @return
     */
    @RequestMapping("/users/updateUserByUsername")
    public Result updateUserByUsername(@RequestBody Student student) {
        boolean existUserByUsername = studentService.existUserByUsername(student.getUsername());
        if (existUserByUsername) {
            // 因为当前接口不支持修改密码，所以此处直接把密码设为null
            student.setPassword(null);
            studentService.updateByUsername(student);
        }
        return Result.success();
    }

    /**
     * 批量删除学生信息
     *
     * @param usernameList
     * @return
     */
    @RequestMapping("/users/delBatch")
    public Result delBatch(@RequestParam("username") List<String> usernameList) {
        studentService.removeBatch(usernameList);
        return Result.success();
    }
}
